using SqlSugar;
using IM.Easy.Core.Services;
using IM.Easy.Core.Utils;
using IM.Easy.Infrastructure.Query;
using IM.Easy.Core.Extensions;
using IM.Easy.Entity.System;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using IM.Easy.CoreServices.Dto;
using IM.Easy.CoreServices.IServices;

namespace IM.Easy.CoreServices.Service
{
    public class ParamsService : AppServices, IParamsService
    {

        public ParamsService(ISqlSugarClient db) : base(db)
        {

        }

        public Params Create(Params entity)
        {
            // TODO: 添加创建逻辑
            return Db.Insertable(entity).ExecuteReturnEntity();
        }

        public Params GetById(long id)
        {
            // TODO: 添加获取逻辑
            return Db.Queryable<Params>().InSingle(id);
        }

        public PageResponse<Params> GetPageList(SysParamsQueryDto query)
        {
            var queryable = Db.Queryable<Params>();
            var exp = Expressionable.Create<Params>();
            exp = exp
                  .AndIF(!string.IsNullOrEmpty(query.Name), s => s.Name.Contains(query.Name))
                  .AndIF(!string.IsNullOrEmpty(query.Value), s => s.Value.Contains(query.Value))
                  .AndIF(query.IsSystem != default, s => SqlFunc.Equals(query.IsSystem, s.IsSystem));
            var page = queryable
                 .Where(exp.ToExpression())
                 .ToPageList<Params, Params>(query);
            return page;
        }

        public bool Update(Params entity)
        {
            // TODO: 添加更新逻辑
            return Db.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params long[] Id)
        {
            // TODO: 添加删除逻辑
            return Db.Deleteable<Params>(Id).ExecuteCommandHasChange();
        }

        public T GetValue<T>(string key, bool nullThrow = false)
        {
            // 获取配置值
            var @params = Db.Queryable<Params>().Single(x => x.Key == key);

            if (@params == null && nullThrow) ExceptionUtils.ThrowArgumentException(key, $"未找到系统配置:{key}");

            // 如果是string类型，直接返回配置值
            if (typeof(T) == typeof(string))
            {
                return (T)(object)@params.Value;
            }

            // 尝试将值转换为T类型
            return (T)Convert.ChangeType(@params.Value, typeof(T));
        }

        public string GetValue(string key, bool nullThrow = false)
        {
            //// 获取配置值
            var value = Db.Queryable<Params>()
                        .Where(s => s.Key == key)
                        .Select(s => s.Value)
                        .Single();

            if (nullThrow && string.IsNullOrEmpty(value)) ExceptionUtils.ThrowArgumentException(key, $"系统配置为空:{key}");

            //            .Single() ?? string.Empty;
            //return @params?.Value ??;
            //var value = Db.Queryable<Params>()
            //            .Where(s => s.Key == key)
            //            .Select(s => s.Value)
            //            .Single() ?? string.Empty;
            return value;
        }

        public List<Params> GetAll()
        {
            return Db.Queryable<Params>().ToList();
        }
    }
}
